﻿using System;
using System.Security.Cryptography;
using Smart.Utils.System2;
using Smart.Utils.App;
using Smart.Utils.Security2;
//using Smart.Service;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.ComponentModel;
using Smart.Utils.Lib;
using Microsoft.VisualBasic;
using Smart.Data.Helper;
using System.Security.AccessControl;
using DevExpress.XtraEditors;
namespace Smart.ERP
{
    public class CODE
    {
        // Fields
        public const string Consys = "HPqrGX/piQz0Xe4u/h/67gQ3uyF/l8Jnvceflfn0g5a097J0Dujgoj8bqG7F7buoYB2Mt+NKyXJ77rNdpJJ69tNga+CVb/KOMzARhvb5JWkXymCWnUJWzzgLCgblpEMy";
        private string m_Active;
        private static string m_Address = "Số 3 Đường số 3 Khu d\x00e2n cư Kiến Ph\x00e1t, Phường 6, TP. T\x00e2n An.";
        private string m_Code;
        private int m_Command;
        private static string m_CompanyName = Environment.MachineName;
        private static string m_ComputerCode;
        private static string m_Contact = Environment.UserName;
        private DateTime m_Created;
        private int m_Day;
        private static int m_DayEnd = 0;
        private string m_Email;
        private string m_Fax;
        private string m_Language;
        private int m_License;
        private DateTime m_Limit;
        private static string m_message = "H\x00e3y đăng k\x00fd phi\x00ean bản thu ph\x00ed để sử dụng đầy đủ t\x00ednh năng v\x00e0 được sự hỗ trợ tốt nhất của ch\x00fang t\x00f4i.";
        private string m_Mobile;
        private int m_Number;
        private string m_Soft_Id;
        private string m_Soft_Name;
        private string m_Tel;
        private DateTime m_Today;
        private static string m_TrialFile = ("Smart." + MyLib.SignSoft() + ".Shared.dll");
        private int m_Type;
        private static int m_TypeActive = 0;
        private static int m_TypeSoft = 0;
        private string m_Version;
        private static string m_Website = "http://www.Smart.com.vn";
        private byte[] number;
        private RandomNumberGenerator rng;
        private static Random rnk = new Random(0x1a);

        // Events
        public event LicenseCheckedEventHanlder LicenseChecked;

        // Methods
        public CODE()
        {
            this.number = new byte[2];
            this.rng = RandomNumberGenerator.Create();
            this.m_Soft_Id = SystemInfoHelper.GetSoftware(MyAssembly.AssemblyTitle);
            this.m_Soft_Name = MyAssembly.AssemblyTitle;
            this.m_Version = MyAssembly.AssemblyVersion;
            this.m_Type = 0;
            this.m_Tel = "";
            this.m_Mobile = "";
            this.m_Fax = "";
            this.m_Email = "";
            this.m_Language = "";
            this.m_Created = DateTime.Now;
            this.m_Limit = DateTime.Now;
            this.m_Day = 0;
            this.m_License = 0;
            this.m_Code = "";
            this.m_Number = 1;
            this.m_Command = 1;
            this.m_Today = DateTime.Now;
            this.m_Active = "";
            m_ComputerCode = "";
        }

        public CODE(string Soft_Id, string Soft_Name, string Version, int Type, string Contact, string Company, string Tel, string Mobile, string Fax, string Email, string Language, DateTime Created, DateTime Limit, int Day, int License, string Code, int Number, int Command, DateTime Today, string Active, string ComputerCode)
        {
            this.number = new byte[2];
            this.rng = RandomNumberGenerator.Create();
            this.m_Soft_Id = Soft_Id;
            this.m_Soft_Name = Soft_Name;
            this.m_Version = Version;
            this.m_Type = Type;
            this.m_Tel = Tel;
            this.m_Mobile = Mobile;
            this.m_Fax = Fax;
            this.m_Email = Email;
            this.m_Language = Language;
            this.m_Created = Created;
            this.m_Limit = Limit;
            this.m_Day = Day;
            this.m_License = License;
            this.m_Code = Code;
            this.m_Number = Number;
            this.m_Command = Command;
            this.m_Today = Today;
            this.m_Active = Active;
            m_ComputerCode = ComputerCode;
        }

        public string ActiveCode()
        {
            this.m_License = Convert.ToInt32(this.m_Code[10].ToString());
            MyEncryption encryption = new MyEncryption();
            string phrase = MyEncryption.Encrypt(this.m_Code, m_ComputerCode, true);
            phrase = encryption.SHA512Encrypt(phrase);
            object obj2 = (((((((phrase.Substring(7, 4) + "-P" + phrase.Substring(0x12, 3)) + "-E" + phrase.Substring(0x18, 3)) + "-R" + phrase.Substring(30, 3)) + "-" + phrase.Substring(0x2a, 4)) + "-F" + phrase.Substring(0x33, 3)) + "-E" + MyEncryption.InverseString(phrase.Substring(0x12, 3))) + "-C" + MyEncryption.InverseString(phrase.Substring(9, 3))) + "-T" + MyEncryption.InverseString(phrase.Substring(0x24, 3));
            return string.Concat(new object[] { obj2, "-", phrase.Substring(0x33, 3)[2], phrase.Substring(0x2a, 4)[3], phrase.Substring(30, 3)[2], phrase.Substring(0x18, 4)[3] });
        }

        public bool ActiveLicense()
        {
            if (!this.CodeMacth())
            {
                return false;
            }
            this.m_License = Convert.ToInt32(this.m_Code[10].ToString());
            return this.MakeCode();
        }

        public bool ActiveMacth()
        {
            return true;
        }

        public bool CheckFree()
        {
            return this.CheckFree(-1);
        }

        public bool CheckFree(int day)
        {
            if (string.IsNullOrEmpty(m_ComputerCode))
            {
                SystemInfoHelper.SetDefault();
                m_ComputerCode = MyRegister.GenID();
            }
            return (this.ReadFile("Smart.Lic", m_ComputerCode) || this.MakeFree(day, 1));
        }

        public bool CheckLicense()
        {
            return this.CheckLicense(30, m_TrialFile, true);
        }

        public bool CheckLicense(bool free)
        {
            return this.CheckLicense(30, m_TrialFile, free);
        }

        public bool CheckLicense(int day, string trialfile)
        {
            SystemInfoHelper.SetDefault();
            m_ComputerCode = MyRegister.GenID();
            m_TrialFile = trialfile;
            if (this.ReadFile("Smart.Lic", m_ComputerCode))
            {
                return true;
            }
            if (this.TrialReadFile("Temp.key", m_ComputerCode))
            {
                if (m_DayEnd <= 0)
                {
                    this.CheckFree();
                }
                return false;
            }
            return this.MakeTrial(day);
        }

        public bool CheckLicense(string trialfile, bool free)
        {
            return this.CheckLicense(30, trialfile, free);
        }

        public bool CheckLicense(int day, string trialfile, bool free)
        {
            SystemInfoHelper.SetDefault();
            m_ComputerCode = MyRegister.GenID();
            m_TrialFile = trialfile;
            if (this.ReadFile("Smart.Lic", m_ComputerCode))
            {
                return true;
            }
            if (this.TrialReadFile("Temp.key", m_ComputerCode))
            {
                if (m_DayEnd <= 0)
                {
                    this.CheckFree();
                    m_message = "Phần Mềm đ\x00e3 hết hạn sử dụng.\n\nTh\x00f4ng Tin Li\x00ean Hệ\n\nC\x00f4ng Ty Phần Mềm Ho\x00e0n Hảo \n\nĐịa chỉ: 41 Đường C\x00e1ch Mạng Th\x00e1ng 8, Phường 1, Thị x\x00e3 T\x00e2n An, Long An \nĐiện thoại: (072) 6251520 \nFax: (072) 6251519\nEmail: sales@Smart.com.vn\nWebsite: www.Smart.com.vn";
                }
                else
                {
                    m_message = "Mua phi\x00ean bản thu ph\x00ed để được hỗ trợ tốt hơn.\n\nTh\x00f4ng Tin Li\x00ean Hệ\n\nC\x00f4ng Ty Phần Mềm Ho\x00e0n Hảo \n\nĐịa chỉ: 41 Đường C\x00e1ch Mạng Th\x00e1ng 8, Phường 1, Thị x\x00e3 T\x00e2n An, Long An \nĐiện thoại: (072) 6251520 \nFax: (072) 6251519\nEmail: sales@Smart.com.vn\nWebsite: www.Smart.com.vn";
                }
                return true;
            }
            return this.MakeTrial(day);
        }

        public bool CheckTrial()
        {
            return this.CheckTrial(30, m_TrialFile);
        }

        public bool CheckTrial(int day)
        {
            return this.CheckTrial(day, m_TrialFile);
        }

        public bool CheckTrial(int day, string trialfile)
        {
            if (!string.IsNullOrEmpty(m_ComputerCode))
            {
                SystemInfoHelper.SetDefault();
                m_ComputerCode = MyRegister.GenID();
            }
            m_TrialFile = trialfile;
            return (this.TrialReadFile("Temp.key", m_ComputerCode) || this.MakeTrial(day));
        }

        public bool CodeMacth()
        {
            return this.CodeMacth(this.m_Code);
        }

        public bool CodeMacth(string code)
        {
            if (code.IndexOf("0000-0000-0000-0000-0000-0000") == -1)
            {
                if (code.IndexOf("1111-1111-1111-1111-1111-1111") != -1)
                {
                    return true;
                }
                this.m_Code = code;
                if (this.m_Code.Length != 0x22)
                {
                    return false;
                }
                if ((((((this.m_Code[4] != '-') | (this.m_Code[9] != '-')) | (this.m_Code[14] != '-')) | (this.m_Code[0x13] != '-')) | (this.m_Code[0x18] != '-')) | (this.m_Code[0x1d] != '-'))
                {
                    return false;
                }
                if (((((((this.m_Code[1] != 'P') | (this.m_Code[7] != 'E')) | (this.m_Code[11] != 'R')) | (this.m_Code[15] != 'F')) | (this.m_Code[0x17] != 'E')) | (this.m_Code[0x1b] != 'C')) | (this.m_Code[0x21] != 'T'))
                {
                    return false;
                }
                string str = this.m_Code.Substring(0x10, 1);
                if ((((((((str != "F") & (str != "S")) & (str != "P")) & (str != "E")) & (str != "T")) & (str != "X")) & (str != "R")) & (str != "B"))
                {
                    return false;
                }
                if (!char.IsNumber(this.m_Code[2]) | !char.IsNumber(this.m_Code[5]))
                {
                    return false;
                }
                if (!char.IsNumber(this.m_Code[12]) | !char.IsNumber(this.m_Code[13]))
                {
                    return false;
                }
                if (char.IsDigit(this.m_Code[0x1c]))
                {
                    return false;
                }
                if (!char.IsNumber(this.m_Code[30]) | !char.IsNumber(this.m_Code[0x1f]))
                {
                    return false;
                }
                if (Convert.ToInt32(this.m_Code[0x12]) != Convert.ToInt32(this.m_Code.Substring(30, 2)))
                {
                    return false;
                }
                if (Convert.ToInt32(this.m_Code[0x1c]) != Convert.ToInt32(this.m_Code.Substring(12, 2)))
                {
                    return false;
                }
                if (!char.IsNumber(this.m_Code[10]))
                {
                    return false;
                }
                if (!Information.IsNumeric(this.m_Code.Substring(20, 3)))
                {
                    return false;
                }
                if (!Information.IsNumeric(this.m_Code.Substring(0x19, 2)))
                {
                    return false;
                }
                if (char.IsNumber(this.m_Code[8]))
                {
                    return false;
                }
                if (Convert.ToChar((int)((Convert.ToInt32(this.m_Code.Substring(0x19, 2)) % 20) + 0x41)) != this.m_Code[8])
                {
                    return false;
                }
                if (char.IsNumber(this.m_Code[3]))
                {
                    return false;
                }
                if (char.IsNumber(this.m_Code[0x20]))
                {
                    return false;
                }
                int num = Convert.ToInt32(this.m_Code[3]) % 15;
                if (this.m_Code[0x20] != Convert.ToChar((int)(num + 0x41)))
                {
                    return false;
                }
            }
            return true;
        }

        public void Execute()
        {
            if (Internet.IsConnectedToInternet())
            {
                SqlHelper helper = new SqlHelper("HPqrGX/piQz0Xe4u/h/67gQ3uyF/l8Jnvceflfn0g5a097J0Dujgoj8bqG7F7buoYB2Mt+NKyXJ77rNdpJJ69tNga+CVb/KOMzARhvb5JWkXymCWnUJWzzgLCgblpEMy", "07071983", true);
                if (helper.Check(true))
                {
                    SystemInfoHelper.SetDefault();
                    m_ComputerCode = MyRegister.GenID();
                    try
                    {
                        if ((m_TypeSoft > 1) && this.ReadFile("Smart.Lic", m_ComputerCode))
                        {
                            LICENSE license = new LICENSE();
                            if (helper.Open() == "OK")
                            {
                                helper.Close();
                                if (license.Get(this.m_Code, m_ComputerCode) == "OK")
                                {
                                    KEYCODE keycode = new KEYCODE();
                                    if (keycode.Get(this.m_Code) == "OK")
                                    {
                                        File.Delete(Application.StartupPath + @"\Smart.key");
                                        this.MakeFree();
                                    }
                                    if (keycode.Blacklist)
                                    {
                                        File.Delete(Application.StartupPath + @"\Smart.key");
                                        this.MakeFree();
                                    }
                                    if (!license.Active)
                                    {
                                        File.Delete(Application.StartupPath + @"\Smart.key");
                                        this.MakeFree();
                                    }
                                }
                                else
                                {
                                    File.Delete(Application.StartupPath + @"\Smart.key");
                                    this.MakeFree();
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        XtraMessageBox.Show("Bản quyền phần mềm n\x00e0y kh\x00f4ng x\x00e1c định được!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        File.Delete(Application.StartupPath + @"\Smart.key");
                    }
                }
            }
        }

        public void Execute(int TimeSpin)
        {
            new Thread(new ThreadStart(this.Execute)).Start();
        }

        private static string FormatNumber(string num)
        {
            if (num.Length == 1)
            {
                return ("00" + num);
            }
            if (num.Length == 2)
            {
                return ("0" + num);
            }
            if (num.Length == 3)
            {
                return num;
            }
            return "000";
        }

        public static string GenKey(int version1, int version2, string typesoft, int num, int type)
        {
            string[] strArray = new string[] { "0P01", "01E1", "1R11", "FFRE", "111E", "11C1", "111T" };
            strArray[0] = GetChar().ToString() + "P" + version1.ToString() + GetLetter().ToString();
            strArray[1] = version2.ToString() + GetChar().ToString() + "E" + GetLetter().ToString();
            strArray[2] = string.Concat(new object[] { type.ToString(), "R", GetNumber().ToString(), GetLetter() });
            strArray[3] = "F" + getTypeSoft(typesoft);
            strArray[4] = FormatNumber(num.ToString()) + "E";
            strArray[5] = GetNumber().ToString() + GetNumber().ToString() + "C" + GetLetter().ToString();
            int num2 = (Convert.ToInt32(strArray[5].Substring(0, 2)) % 20) + 0x41;
            strArray[1] = strArray[1].Remove(3);
            strArray[1] = strArray[1] + Convert.ToChar(num2).ToString();
            strArray[2] = strArray[2].Remove(2);
            strArray[2] = strArray[2] + Convert.ToInt32(strArray[5][3]).ToString();
            num2 = (Convert.ToInt32(strArray[0][3]) % 15) + 0x41;
            strArray[6] = Convert.ToInt32(strArray[3][3]).ToString() + Convert.ToChar(num2) + "T";
            return string.Join("-", strArray);
        }

        internal static char GetChar()
        {
            if (rnk.Next(2) > 1)
            {
                return GetChar();
            }
            return GetNumber();
        }

        private static char GetChar(int seed, int add)
        {
            return Convert.ToChar((int)(rnk.Next(seed) + add));
        }

        public static string GetComputerCode()
        {
            SystemInfoHelper.SetDefault();
            m_ComputerCode = MyRegister.GenID();
            return m_ComputerCode;
        }

        internal static char GetLetter()
        {
            return GetChar(0x1a, 0x41);
        }

        internal static char GetNumber()
        {
            return GetChar(9, 0x30);
        }

        private static string getTypeSoft(string typesoft)
        {
            if (typesoft.StartsWith("PRO"))
            {
                return ("P" + GetNumber().ToString() + GetNumber().ToString());
            }
            if (typesoft.StartsWith("FRE"))
            {
                return ("F" + GetNumber().ToString() + GetNumber().ToString());
            }
            if (typesoft.StartsWith("STA"))
            {
                return ("S" + GetNumber().ToString() + GetNumber().ToString());
            }
            if (typesoft.StartsWith("ENT"))
            {
                return ("E" + GetNumber().ToString() + GetNumber().ToString());
            }
            if (typesoft.StartsWith("ENB"))
            {
                return "ENB";
            }
            if (typesoft.StartsWith("EXP"))
            {
                return ("R" + GetNumber().ToString() + GetNumber().ToString());
            }
            if (typesoft.StartsWith("EXT"))
            {
                return ("X" + GetNumber().ToString() + GetNumber().ToString());
            }
            return ("T" + GetNumber().ToString() + GetNumber().ToString());
        }

        private static int GetTypeSoft(string code)
        {
            if (code.Length == 0)
            {
                return 1;
            }
            string str = code.Substring(0x10, 3);
            if (str.StartsWith("F"))
            {
                return 1;
            }
            if (str.StartsWith("S"))
            {
                return 200;
            }
            if (str.StartsWith("P"))
            {
                return 300;
            }
            if (str.StartsWith("E"))
            {
                return 400;
            }
            if (str.StartsWith("R"))
            {
                return 500;
            }
            if (str.StartsWith("X"))
            {
                return 0x12d;
            }
            if (str.StartsWith("B"))
            {
                return 0x12e;
            }
            if (str.StartsWith("ENB"))
            {
                return 0x1f5;
            }
            return 0;
        }

        private void InvokeLicenseChecked()
        {
            LicenseCheckedEventHanlder licenseChecked = this.LicenseChecked;
            if (licenseChecked != null)
            {
                licenseChecked(this);
            }
        }

        public bool MakeCode()
        {
            MyEncryption encryption = new MyEncryption();
            string phrase = MyEncryption.Encrypt(this.m_Code, m_ComputerCode, true);
            phrase = encryption.SHA512Encrypt(phrase);
            object obj2 = (((((((phrase.Substring(7, 4) + "-P" + phrase.Substring(0x12, 3)) + "-E" + phrase.Substring(0x18, 3)) + "-R" + phrase.Substring(30, 3)) + "-" + phrase.Substring(0x2a, 4)) + "-F" + phrase.Substring(0x33, 3)) + "-E" + MyEncryption.InverseString(phrase.Substring(0x12, 3))) + "-C" + MyEncryption.InverseString(phrase.Substring(9, 3))) + "-T" + MyEncryption.InverseString(phrase.Substring(0x24, 3));
            return (string.Concat(new object[] { obj2, "-", phrase.Substring(0x33, 3)[2], phrase.Substring(0x2a, 4)[3], phrase.Substring(30, 3)[2], phrase.Substring(0x18, 4)[3] }).CompareTo(this.m_Active) == 0);
        }

        public bool MakeFree()
        {
            return this.MakeFree(-1, 1);
        }

        private bool MakeFree(int free, int typesoft)
        {
            this.m_Day = free;
            m_DayEnd = this.m_Day;
            this.m_License = 1;
            m_TypeActive = this.m_License;
            this.m_Limit = DateTime.Now.AddDays((double)free);
            this.m_Type = typesoft;
            m_TypeSoft = typesoft;
            if (!this.WriteFile("Smart.Lic"))
            {
                return false;
            }
            if (this.ReadFile("Smart.Lic", m_ComputerCode))
            {
                return false;
            }
            return true;
        }

        public bool MakeTrial()
        {
            CODE code = new CODE();
            return this.MakeTrial(code.Day);
        }

        private bool MakeTrial(int ngay)
        {
            this.m_Limit = DateTime.Now.AddDays((double)ngay);
            if (Internet.IsConnectedToInternet())
            {
                LICENSE license = new LICENSE();
                if (license.Get(MyLib.SignSoft() + "-0000-0000-0000-0000-0000-0000", m_ComputerCode) == "OK")
                {
                    if (DateAndTime.DateDiff(DateInterval.Day, license.DateActive, DateTime.Now, FirstDayOfWeek.Monday, FirstWeekOfYear.System) > 30L)
                    {
                        m_message = "Phần Mềm đ\x00e3 hết hạn sử dụng.\n\nTh\x00f4ng Tin Li\x00ean Hệ\n\nC\x00f4ng Ty Phần Mềm Ho\x00e0n Hảo \n\nĐịa chỉ: 41 Đường C\x00e1ch Mạng Th\x00e1ng 8, Phường 1, Thị x\x00e3 T\x00e2n An, Long An \nĐiện thoại: (072) 6251520 \nFax: (072) 6251519\nEmail: sales@Smart.com.vn\nWebsite: www.Smart.com.vn";
                        this.MakeFree();
                    }
                }
                else if (license.Insert(MyAssembly.AssemblyTitle, MyLib.SignSoft() + "-0000-0000-0000-0000-0000-0000", m_ComputerCode, true, MyAssembly.AssemblyVersion) == "OK")
                {
                    this.InvokeLicenseChecked();
                }
            }
            if (!this.TrialWriteFile("Temp.key"))
            {
                return false;
            }
            if (this.TrialReadFile("Temp.key", m_ComputerCode))
            {
                return false;
            }
            return true;
        }

        public static void Notify()
        {
            XtraMessageBox.Show("Chức năng n\x00e0y kh\x00f4ng được hỗ trợ trong phi\x00ean bản miễn ph\x00ed (Free Edition)\nĐể sử dụng chức năng n\x00e0y vui l\x00f2ng đăng k\x00fd phi\x00ean bản thu ph\x00ed.\n\nTh\x00f4ng Tin Li\x00ean Hệ\n\nC\x00f4ng Ty Phần Mềm Ho\x00e0n Hảo \n\nĐịa chỉ: 41 Đường C\x00e1ch Mạng Th\x00e1ng 8, Phường 1, Thị x\x00e3 T\x00e2n An, Long An \nĐiện thoại: (072) 6251520 \nFax: (072) 6251519\nEmail: sales@Smart.com.vn\nWebsite: www.Smart.com.vn", "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void ProgressCallBackEncrypt(int min, int max, int value)
        {
        }

        public bool ReadFile(string filePath, string key)
        {
            filePath = Application.StartupPath + @"\" + filePath;
            FileInfo info = new FileInfo(Application.StartupPath + @"\Smart.key");
            if (!info.Exists)
            {
                return false;
            }
            try
            {
                CryptoProgressCallBack callback = new CryptoProgressCallBack(this.ProgressCallBackEncrypt);
                CryptoHelper.DecryptFile(Application.StartupPath + @"\Smart.key", filePath, key, callback);
                FileStream input = File.OpenRead(filePath);
                BinaryReader reader = new BinaryReader(input);
                this.m_Active = MyEncryption.Decrypt(reader.ReadString(), key, true);
                m_Address = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Code = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Command = Convert.ToInt32(MyEncryption.Decrypt(reader.ReadString(), key, true));
                CompanyName = MyEncryption.Decrypt(reader.ReadString(), key, true);
                m_ComputerCode = MyEncryption.Decrypt(reader.ReadString(), key, true);
                m_Contact = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Created = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(reader.ReadString(), key, true)));
                this.m_Day = Convert.ToInt32(MyEncryption.Decrypt(reader.ReadString(), key, true));
                m_DayEnd = this.m_Day;
                this.m_Email = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Fax = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Language = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_License = Convert.ToInt32(MyEncryption.Decrypt(reader.ReadString(), key, true));
                m_TypeActive = this.m_License;
                this.m_Limit = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(reader.ReadString(), key, true)));
                this.m_Mobile = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Number = Convert.ToInt32(MyEncryption.Decrypt(reader.ReadString(), key, true));
                this.m_Soft_Id = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Soft_Name = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Tel = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Today = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(reader.ReadString(), key, true)));
                this.m_Type = Convert.ToInt32(MyEncryption.Decrypt(reader.ReadString(), key, true));
                m_TypeSoft = this.m_Type;
                this.m_Version = MyEncryption.Decrypt(reader.ReadString(), key, true);
                m_Website = MyEncryption.Decrypt(reader.ReadString(), key, true);
                reader.Close();
                input.Close();
                File.Delete(filePath);
            }
            catch (Exception)
            {
                info.Delete();
                return false;
            }
            return true;
        }

        public int Rnk()
        {
            this.rng.GetBytes(this.number);
            return BitConverter.ToInt16(this.number, 0);
        }

        public void SetTrialFile(string trialfile)
        {
            m_TrialFile = trialfile;
        }

        public bool TrialReadFile(string filePath, string key)
        {
            filePath = Application.StartupPath + @"\" + filePath;
            string fileName = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) + @"\Smart Shared\" + m_TrialFile;
            FileInfo info = new FileInfo(fileName);
            if (!info.Exists)
            {
                return false;
            }
            try
            {
                CryptoProgressCallBack callback = new CryptoProgressCallBack(this.ProgressCallBackEncrypt);
                CryptoHelper.DecryptFile(fileName, filePath, key, callback);
                FileStream input = File.OpenRead(filePath);
                BinaryReader reader = new BinaryReader(input);
                this.m_Active = MyEncryption.Decrypt(reader.ReadString(), key, true);
                m_Address = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Code = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Command = Convert.ToInt32(MyEncryption.Decrypt(reader.ReadString(), key, true));
                CompanyName = MyEncryption.Decrypt(reader.ReadString(), key, true);
                m_ComputerCode = MyEncryption.Decrypt(reader.ReadString(), key, true);
                m_Contact = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Created = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(reader.ReadString(), key, true)));
                this.m_Day = Convert.ToInt32(MyEncryption.Decrypt(reader.ReadString(), key, true));
                this.m_Email = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Fax = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Language = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_License = Convert.ToInt32(MyEncryption.Decrypt(reader.ReadString(), key, true));
                m_TypeActive = 0;
                this.m_Limit = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(reader.ReadString(), key, true)));
                m_DayEnd = Convert.ToInt32(DateAndTime.DateDiff(DateInterval.Day, DateTime.Now, this.m_Limit, FirstDayOfWeek.Monday, FirstWeekOfYear.System));
                this.m_Mobile = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Number = Convert.ToInt32(MyEncryption.Decrypt(reader.ReadString(), key, true));
                this.m_Soft_Id = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Soft_Name = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Tel = MyEncryption.Decrypt(reader.ReadString(), key, true);
                this.m_Today = new DateTime(Convert.ToInt64(MyEncryption.Decrypt(reader.ReadString(), key, true)));
                this.m_Type = Convert.ToInt32(MyEncryption.Decrypt(reader.ReadString(), key, true));
                m_TypeSoft = 0;
                this.m_Version = MyEncryption.Decrypt(reader.ReadString(), key, true);
                m_Website = MyEncryption.Decrypt(reader.ReadString(), key, true);
                reader.Close();
                input.Close();
                File.Delete(filePath);
                m_message = "Phần Mềm c\x00f2n " + m_DayEnd + " (ng\x00e0y) hạn sử dụng.\n\nTh\x00f4ng Tin Li\x00ean Hệ\n\nC\x00f4ng Ty Phần Mềm Ho\x00e0n Hảo \n\nĐịa chỉ: 41 Đường C\x00e1ch Mạng Th\x00e1ng 8, Phường 1, Thị x\x00e3 T\x00e2n An, Long An \nĐiện thoại: (072) 6251520 \nFax: (072) 6251519\nEmail: sales@Smart.com.vn\nWebsite: www.Smart.com.vn";
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public bool TrialWriteFile(string filePath)
        {
            filePath = Application.StartupPath + @"\" + filePath;
            try
            {
                FileStream output = File.Create(filePath);
                BinaryWriter writer = new BinaryWriter(output);
                string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) + @"\Smart Shared";
                if (!Directory.Exists(path))
                {
                    DirectoryInfo info = new DirectoryInfo(path);
                    info.Create();
                    info.GetAccessControl().SetAccessRule(new FileSystemAccessRule(Environment.UserDomainName + @"\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow));
                }
                string str2 = path + @"\" + m_TrialFile;
                if (File.Exists(str2))
                {
                    File.Delete(str2);
                }
                for (int i = 0; i < 30; i++)
                {
                    writer.Write(MyEncryption.Encrypt(this.m_Active, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(m_Address, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Code, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Command.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(CompanyName, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(m_ComputerCode, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(m_Contact, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Created.Ticks.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Day.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Email, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Fax, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Language, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_License.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Limit.Ticks.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Mobile, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Number.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Soft_Id, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Soft_Name, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Tel, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Today.Ticks.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Type.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Version, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(m_Website, m_ComputerCode, true));
                }
                writer.Close();
                output.Close();
                CryptoProgressCallBack callback = new CryptoProgressCallBack(this.ProgressCallBackEncrypt);
                CryptoHelper.EncryptFile(filePath, str2, m_ComputerCode, callback);
                File.Delete(filePath);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public bool WriteFile(string filePath)
        {
            filePath = Application.StartupPath + @"\" + filePath;
            try
            {
                FileStream output = File.Create(filePath);
                BinaryWriter writer = new BinaryWriter(output);
                File.Delete(Application.StartupPath + @"\Smart.key");
                this.m_Type = GetTypeSoft(this.m_Code);
                for (int i = 0; i < 30; i++)
                {
                    writer.Write(MyEncryption.Encrypt(this.m_Active, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(m_Address, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Code, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Command.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(CompanyName, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(m_ComputerCode, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(m_Contact, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Created.Ticks.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Day.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Email, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Fax, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Language, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_License.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Limit.Ticks.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Mobile, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Number.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Soft_Id, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Soft_Name, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Tel, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Today.Ticks.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Type.ToString(), m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(this.m_Version, m_ComputerCode, true));
                    writer.Write(MyEncryption.Encrypt(m_Website, m_ComputerCode, true));
                }
                writer.Close();
                output.Close();
                CryptoProgressCallBack callback = new CryptoProgressCallBack(this.ProgressCallBackEncrypt);
                CryptoHelper.EncryptFile(filePath, Application.StartupPath + @"\Smart.key", m_ComputerCode, callback);
                File.Delete(filePath);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        // Properties
        [Category("Column"), DisplayName("Active")]
        public string Active
        {
            get
            {
                return this.m_Active;
            }
            set
            {
                this.m_Active = value;
            }
        }

        [Category("Column"), DisplayName("Address")]
        public static string Address
        {
            get
            {
                return m_Address;
            }
            set
            {
                m_Address = value;
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return "2.5.0.0";
            }
        }

        [Category("Column"), DisplayName("Code")]
        public string Code
        {
            get
            {
                return this.m_Code;
            }
            set
            {
                this.m_Code = value;
            }
        }

        [Category("Column"), DisplayName("Command")]
        public int Command
        {
            get
            {
                return this.m_Command;
            }
            set
            {
                this.m_Command = value;
            }
        }

        [Category("Column"), DisplayName("Company")]
        public static string CompanyName
        {
            get
            {
                return m_CompanyName;
            }
            set
            {
                m_CompanyName = value;
            }
        }

        [DisplayName("ComputerCode"), Category("Column")]
        public static string ComputerCode
        {
            get
            {
                return m_ComputerCode;
            }
            set
            {
                m_ComputerCode = value;
            }
        }

        [DisplayName("Contact"), Category("Column")]
        public static string Contact
        {
            get
            {
                return m_Contact;
            }
            set
            {
                m_Contact = value;
            }
        }

        public string Copyright
        {
            get
            {
                return "C\x00f4ng Ty Phần Mềm Ho\x00e0n Hảo";
            }
        }

        [DisplayName("Created"), Category("Column")]
        public DateTime Created
        {
            get
            {
                return this.m_Created;
            }
            set
            {
                this.m_Created = value;
            }
        }

        [DisplayName("Day"), Category("Column")]
        public int Day
        {
            get
            {
                return this.m_Day;
            }
            set
            {
                this.m_Day = value;
            }
        }

        public static int DayEnd
        {
            get
            {
                return m_DayEnd;
            }
        }

        [DisplayName("Email"), Category("Column")]
        public string Email
        {
            get
            {
                return this.m_Email;
            }
            set
            {
                this.m_Email = value;
            }
        }

        [Category("Column"), DisplayName("Fax")]
        public string Fax
        {
            get
            {
                return this.m_Fax;
            }
            set
            {
                this.m_Fax = value;
            }
        }

        [Category("Column"), DisplayName("Language")]
        public string Language
        {
            get
            {
                return this.m_Language;
            }
            set
            {
                this.m_Language = value;
            }
        }

        [DisplayName("License"), Category("Column")]
        public int License
        {
            get
            {
                return this.m_License;
            }
            set
            {
                this.m_License = value;
            }
        }

        [Category("Column"), DisplayName("Limit")]
        public DateTime Limit
        {
            get
            {
                return this.m_Limit;
            }
            set
            {
                this.m_Limit = value;
            }
        }

        public static string Message
        {
            get
            {
                return m_message;
            }
        }

        [DisplayName("Mobile"), Category("Column")]
        public string Mobile
        {
            get
            {
                return this.m_Mobile;
            }
            set
            {
                this.m_Mobile = value;
            }
        }

        [DisplayName("Number"), Category("Column")]
        public int Number
        {
            get
            {
                return this.m_Number;
            }
            set
            {
                this.m_Number = value;
            }
        }

        public string Product
        {
            get
            {
                return "Class CODE";
            }
        }

        [DisplayName("Soft_Id"), Category("Primary Key")]
        public string Soft_Id
        {
            get
            {
                return this.m_Soft_Id;
            }
            set
            {
                this.m_Soft_Id = value;
            }
        }

        [Category("Column"), DisplayName("Soft_Name")]
        public string Soft_Name
        {
            get
            {
                return this.m_Soft_Name;
            }
            set
            {
                this.m_Soft_Name = value;
            }
        }

        public static string SoftInfo
        {
            get
            {
                if (m_TypeSoft == 0)
                {
                    return "Trial Edition";
                }
                if (m_TypeSoft == 1)
                {
                    return "Free Edition";
                }
                if ((m_TypeSoft >= 200) & (m_TypeSoft <= 0x12b))
                {
                    return "Standard Edition";
                }
                if ((m_TypeSoft >= 300) & (m_TypeSoft <= 0x18f))
                {
                    return "Professional Edition";
                }
                if ((m_TypeSoft >= 400) & (m_TypeSoft <= 0x1f3))
                {
                    return "Enterprise Edition";
                }
                if ((m_TypeSoft >= 500) & (m_TypeSoft <= 0x257))
                {
                    return "Express Edition";
                }
                if ((m_TypeSoft >= 600) & (m_TypeSoft <= 0x2bb))
                {
                    return "Extanded(Packaged) Edition";
                }
                return "Unknown Edition";
            }
        }

        [Category("Column"), DisplayName("Tel")]
        public string Tel
        {
            get
            {
                return this.m_Tel;
            }
            set
            {
                this.m_Tel = value;
            }
        }

        [Category("Column"), DisplayName("Today")]
        public DateTime Today
        {
            get
            {
                return this.m_Today;
            }
            set
            {
                this.m_Today = value;
            }
        }

        [Category("Column"), DisplayName("Type")]
        public int Type
        {
            get
            {
                return this.m_Type;
            }
            set
            {
                this.m_Type = value;
            }
        }

        public static int TypeActive
        {
            get
            {
                return m_TypeActive;
            }
        }

        public static int TypeSoft
        {
            get
            {
                return m_TypeSoft;
            }
            set
            {
                m_TypeSoft = value;
            }
        }

        [DisplayName("Version"), Category("Column")]
        public string Version
        {
            get
            {
                return this.m_Version;
            }
            set
            {
                this.m_Version = value;
            }
        }

        [DisplayName("Website"), Category("Column")]
        public static string Website
        {
            get
            {
                return m_Website;
            }
            set
            {
                m_Website = value;
            }
        }

        // Nested Types
        public delegate void LicenseCheckedEventHanlder(object sender);
    }


}

